package com.nidu.demo.mail.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.mail.convertor.MailAccountConvertor;
import com.nidu.demo.mail.dto.MailAccountCO;
import com.nidu.demo.mail.gateway.MailAccountGateway;
import com.nidu.demo.mail.model.MailAccount;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 邮件账号单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MailAccountSingleQryExe {

    private final MailAccountGateway gateway;
    private final MailAccountConvertor convertor;

    public SingleResponse<MailAccountCO> execute(Long id) {
        // 1. 根据ID查询邮件账号
        MailAccount mailAccount = gateway.getById(id);
        if (mailAccount == null) {
            throw new BizException(ErrorCodeConstants.MAIL_ACCOUNT_NOT_FOUND.getCode(), ErrorCodeConstants.MAIL_ACCOUNT_NOT_FOUND.getMessage());
        }

        // 2. 转换为DTO返回
        MailAccountCO mailAccountCO = convertor.toClientObject(mailAccount);
        return SingleResponse.of(mailAccountCO);
    }

}
